package com.example.doer.reading.AppActivities;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.ab.fragment.AbFragment;
import com.ab.http.AbHttpUtil;
import com.ab.http.AbStringHttpResponseListener;
import com.ab.util.AbDialogUtil;
import com.ab.util.AbToastUtil;
import com.ab.view.pullview.AbPullToRefreshView;
import com.example.doer.reading.AppAdapters.MesListAdapter;
import com.example.doer.reading.AppDataModel.MyMessage;
import com.example.doer.reading.R;
import com.example.doer.reading.global.MyApplication;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class MessageFragment extends AbFragment {
    private AbPullToRefreshView abPullToRefreshView=null;
    private AbHttpUtil abHttpUtil;
    private String url;
    private ListView listView;
    public MessageFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_message, container, false);
        abHttpUtil=AbHttpUtil.getInstance(getActivity());
        listView=(ListView) view.findViewById(R.id.message_list);
        url="http://192.168.191.1/Reading/MessageServlet?Action=down&userID="+ MyApplication.user.getUserID();
        httpGetMessage();

        abPullToRefreshView=(AbPullToRefreshView)view.findViewById(R.id.mPullRefreshView);
        abPullToRefreshView.setOnHeaderRefreshListener(new AbPullToRefreshView.OnHeaderRefreshListener() {
            @Override
            public void onHeaderRefresh(final AbPullToRefreshView abPullToRefreshView) {
               httpGetMessage();
            }
        });
        abPullToRefreshView.setOnFooterLoadListener(new AbPullToRefreshView.OnFooterLoadListener() {
            @Override
            public void onFooterLoad(AbPullToRefreshView abPullToRefreshView) {
                abPullToRefreshView.onHeaderRefreshFinish();
            }
        });
        return view;
    }


    public void httpGetMessage(){
        abHttpUtil.get(url, new AbStringHttpResponseListener() {
            @Override
            public void onSuccess(int i, String s) {
                List <MyMessage> messageList =new ArrayList<MyMessage>();
                int state;//0为点赞，1为收藏，2为回复，3为关注
                try {
                    JSONObject jsonObject=new JSONObject(s);
                    JSONArray jsonArray=jsonObject.getJSONArray("Message");
                    for (int j=0;j<jsonArray.length();j++){
                        JSONObject message=jsonArray.getJSONObject(j);

                        String UserID=message.getString("FromUserID");
                        String Date =message.getString("date");
                        String content=message.getString("messageContent");

                        String[] blocks=content.split("_");
                        int index_userName_start=content.indexOf("用户");
                        int index_userName_end=content.indexOf("觉得");
                        String UserName=blocks[0].substring(index_userName_start+2,index_userName_end);
                        String MessageValue=blocks[1];
                        MyMessage myMessage=new MyMessage();
                        myMessage.setUserID(UserID);
                        myMessage.setTitle(UserName);
                        myMessage.setContent(MessageValue);
                        myMessage.setInfo(Date);

                        if (blocks[2].contains("赞")) {
                            state = 0;
                            myMessage.setResID(R.mipmap.thumbup);
                        }else if (blocks[2].contains("收藏")){
                            state=1;
                            myMessage.setResID(R.mipmap.collect);
                        }else if (blocks[2].contains("回复")){
                            state=2;
                            myMessage.setResID(R.mipmap.reply);
                        }else if (blocks[2].contains("关注")){
                            state=3;
                            myMessage.setResID(R.mipmap.focus);
                        }
                        messageList.add(myMessage);
                    }
                    MesListAdapter mesListAdapter=new MesListAdapter(getActivity(),messageList,R.layout.message_listview);
                    listView.setAdapter(mesListAdapter);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                AbDialogUtil.removeDialog(getActivity());
                abPullToRefreshView.onHeaderRefreshFinish();
            }

            @Override
            public void onStart() {
                AbDialogUtil.showProgressDialog(getActivity(),0,"正在加载...");
            }

            @Override
            public void onFinish() {

            }

            @Override
            public void onFailure(int i, String s, Throwable throwable) {
                AbDialogUtil.removeDialog(getActivity());
                AbToastUtil.showToast(getActivity(),throwable.getMessage());
                abPullToRefreshView.onHeaderRefreshFinish();
            }
        });


    }

}
